package com.tnsw.noxnox;

import com.tnsw.noxnox.R;

import android.support.v4.app.Fragment;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalFocusChangeListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class WhatsHot extends Fragment implements OnPreDrawListener, OnGlobalLayoutListener, OnClickListener {

	private String logName = "WhatsHot";
	private ViewGroup root;
	
	public WhatsHot() {
		Log.d(logName, "constructor()");
		
	}
	
	public static Fragment newInstance(Context context) {
		Log.d("WhatsHot", "newInstance");
		WhatsHot f = new WhatsHot();
		return(f);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(logName, "onCreateView()");
		this.root = (ViewGroup) inflater.inflate(R.layout.whatshot_layout, null);		
		
		this.root.getViewTreeObserver().addOnPreDrawListener(this);
		this.root.getViewTreeObserver().addOnGlobalLayoutListener(this);
		
		Button btnButton = (Button) this.root.findViewById(R.id.btnButton);
		if (btnButton != null) {
			btnButton.setOnClickListener(this);	
		} else {
			Log.w(logName, "why btnButton == null!!");
		}
		
		return(root);
	}

	@Override
	public boolean onPreDraw() {
		/*
		ImageView imgWhatsHot = (ImageView) this.root.findViewById(R.id.imgWhatsHot);
		TextView tvWhatsHot = (TextView) this.root.findViewById(R.id.tvWhatsHot);
		
		if (imgWhatsHot != null) Log.d(logName, "Found imgWhatsHot");
		if (tvWhatsHot != null) Log.d(logName, "Found tvWhatsHot");
		
		Log.d(logName, "tvWhatsHot.getLayoutParams()=" + tvWhatsHot.getLayoutParams().toString());
		int textHeight = tvWhatsHot.getHeight();
		int imgHeight = imgWhatsHot.getHeight();
		int newTextTop = imgHeight-textHeight;
		
		tvWhatsHot.setTop(200);
		tvWhatsHot.setHeight(500);
						
		Log.d(logName, String.valueOf(newTextTop));
		*/
		return true;
	}

	@Override
	public void onGlobalLayout() {
		Log.d(logName, "onGlobalLayout()");
		ImageView imgWhatsHot = (ImageView) this.root.findViewById(R.id.imgWhatsHot);
		TextView tvWhatsHot = (TextView) this.root.findViewById(R.id.tvWhatsHot);
		
		if (imgWhatsHot != null) Log.d(logName, "Found imgWhatsHot");
		if (tvWhatsHot != null) Log.d(logName, "Found tvWhatsHot");
		
		Log.d(logName, "tvWhatsHot.getLayoutParams()=" + tvWhatsHot.getLayoutParams().toString());
		int textHeight = tvWhatsHot.getHeight();
		int imgHeight = imgWhatsHot.getHeight();
		int newTextTop = imgHeight-textHeight;
		
		Log.d(logName, "textHeight=" + textHeight);
		Log.d(logName, "imgHeight=" + imgHeight);
		
		//tvWhatsHot.setTop(200);
		//tvWhatsHot.setHeight(500);
						
		Log.d(logName, String.valueOf(newTextTop));		
	}

	@Override
	public void onClick(View v) {
		Button btnButton = (Button) this.root.findViewById(R.id.btnButton);
		
		if (v == btnButton) {
			ImageView imgWhatsHot = (ImageView) this.root.findViewById(R.id.imgWhatsHot);
			int imgHeight = imgWhatsHot.getHeight();
			Log.d(logName, "imgHeight=" + imgHeight);
			imgWhatsHot.setTop(imgWhatsHot.getTop()+1);
			
			imgWhatsHot.setBottom(imgWhatsHot.getBottom()+1);
			
			// LayoutParams layout = imgWhatsHot.getLayoutParams();			
			// imgWhatsHot.setLayoutParams(layout);
		}
		
	}
}
